package com.sptpc.demo.controller;


import com.sptpc.demo.controller.requests.ModifyStudentRequest;
import com.sptpc.demo.model.Drink;
import com.sptpc.demo.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/stu")
public class StudentController {

//    新增
    @PostMapping("/add")
    public Student addNewStudent(@RequestBody Student student){
        LocalDate localDate=LocalDate.of(2019,9,1);
        log.info("添加的学生信息: {}",student);
        //后期使用数据库insert
//        Student addStudent=Student.builder().id(4L)
//                .name("张三")
//                .adminssionDate(localDate)
//                .major("移动应用开发")
//                .build();
//        return addStudent;
        return Student.builder().id(4L)
                .name("张三")
                .adminssionDate(localDate)
                .major("移动应用开发")
                .build();
    }

    //删除
    @DeleteMapping("/del/{id}")
    public String deleteStudent(@PathVariable Long id){
        log.info("要删除的人的id: {}",id);
        return "删除成功";
    }

    //查询
    @GetMapping("/all")
    public List<Student> getAll(){
        LocalDate localDate=LocalDate.of(2019,9,1);
        //mock数据 - 后期使用数据库查询
        List<Student> results=new ArrayList<>();
        results.add(Student.builder().id(1L)
                .name("张三")
                .adminssionDate(localDate)
                .major("移动应用开发")
                .build());
        results.add(Student.builder().id(2L)
                .name("李四")
                .adminssionDate(localDate)
                .major("移动应用开发")
                .build());
        results.add(Student.builder().id(3L)
                .name("王五")
                .adminssionDate(localDate)
                .major("移动应用开发")
                .build());
        return results;
    }

    //查询单个
    @GetMapping("/{id}")
    public Student getStudent(@PathVariable Integer id){
        LocalDate localDate=LocalDate.of(2019,9,1);
        log.info("查询饮品的id: {}", id);
        //    后期使用数据库查询
        // select .... where id = $id
        Student student=Student.builder().id(1L)
                .name("王五")
                .adminssionDate(localDate)
                .major("移动应用开发")
                .build();
        return student;
    }

    @PutMapping("/update")
    public Student modifyStudent(@RequestBody ModifyStudentRequest request){
        LocalDate localDate=LocalDate.of(2019,9,1);
        log.info("要修改的信息: {}", request);
        //后期使用数据库update
        Student student=Student.builder().id(1L)
                .name("王五")
                .adminssionDate(localDate)
                .major("移动应用开发")
                .build();
        return student;
    }

    @PostMapping(value = "/batch",
            consumes ={MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<Student> batchAddNewStudent(MultipartFile file){
        List<Student> results=new ArrayList<>();
        try {
            BufferedReader reader=new BufferedReader(new InputStreamReader(
                    file.getInputStream()));
            String line="";
            while ((line=reader.readLine())!=null) {
                //解析一行
                String[] args=line.split(",");
                Student student=Student.builder()
                        .id(Long.parseLong(args[0]))
                        .name(args[1])
                        .adminssionDate(LocalDate.parse(args[2]))
                        .major(args[3])
                        .build();
                results.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }
}
